package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Post;
import com.sc.pojo.Posttype;
import com.sc.pojo.User;
import com.sc.service.PostService;
import com.sc.service.PosttypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostService ps;
    @Autowired
    PosttypeService pts;
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        PageInfo<Post> p;
        if(session.getAttribute("search")==null){
            //查所有数据
            p=ps.show(pageNum,4);
        }else {
            //有条件的分页
            Post post=(Post) session.getAttribute("search");
            p=ps.search(pageNum,4,post);
        }
        m.put("p",p);
        return "/post/index";
    }
    @RequestMapping("/mgrShow")
    public String mgrShow(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        PageInfo<Post> p;
        if(session.getAttribute("search")==null){
            //查所有数据
            p=ps.mgrShow(pageNum,4);
        }else {
            //有条件的分页
            Post post=(Post) session.getAttribute("search");
            p=ps.mgrSearch(pageNum,4,post);
        }
        m.put("p",p);
        return "/post/postMgr/list";
    }

    @RequestMapping("/add")
    public String add(Post p,Integer writerid){
        p.setWriterid(writerid);
        p.setTime(new Date());
        ps.add(p);
        return "redirect:/post/show";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        //删除该帖子
        ps.del(id);
        return "redirect:/post/myPost";
    }
    //后台删除
    @RequestMapping("/mgrDel")
    public String mgrDel(Integer id){
        //删除该帖子
        ps.del(id);
        return "redirect:/post/mgrShow";
    }
    @RequestMapping("/delAll")
    public String delAll(Integer[] ids){
        if (ids != null) {
            ps.delAll(ids);
        }

        return "redirect:/post/myPost";
    }
    //后台批量删除
    @RequestMapping("/mgrDelAll")
    public String mgrDelAll(Integer[] ids){
        if (ids != null) {
            ps.delAll(ids);
        }
        return "redirect:/post/mgrShow";
    }

    @RequestMapping("/toUpdate")
     public String toUpdate(Integer id,Map m){
        //找到该帖子
        Post p=ps.lookPost(id);
        m.put("p",p);
        return "/post/updatePost";
    }
    @RequestMapping("/update")
    public String update(Post p){
        ps.update(p);
        return "redirect:/post/myPost";
    }

    @RequestMapping("/myPost")
    public String myPost(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        User nowUser=(User) session.getAttribute("theUser");
        PageInfo<Post> p=ps.showMine(nowUser.getId(),pageNum,4);
        m.put("p",p);
        return "/post/myPost";
    }
    @RequestMapping("/supportPost")
    public String supportPost(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        User nowUser=(User) session.getAttribute("theUser");
        PageInfo<Post> p=ps.supportPost(nowUser.getId(),pageNum,4);
        m.put("p",p);
        return "/post/supportPost";
    }
    @RequestMapping("/commentPost")
    public String commentPost(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        User nowUser=(User) session.getAttribute("theUser");
        PageInfo<Post> p=ps.commentPost(nowUser.getId(),pageNum,4);
        m.put("p",p);
        return "/post/commentPost";
    }

    @RequestMapping("/clickPost")
    public String clickPost(@RequestParam(defaultValue = "1") Integer pageNum, Map m,HttpSession session){
        User nowUser=(User) session.getAttribute("theUser");
        PageInfo<Post> p=ps.clickPost(nowUser.getId(),pageNum,4);
        m.put("p",p);
        return "/post/clickPost";
    }

    @RequestMapping("/search")
    public String search(@RequestParam(defaultValue = "1") Integer pageNum, Map m,Post post,HttpSession session){
        if(post!=(session.getAttribute("search"))){
            session.setAttribute("search",post);
        }
        PageInfo<Post> p=ps.search(pageNum,4,post);
        m.put("p",p);
        return "/post/index";
    }
    @RequestMapping("/mgrSearch")
    public String mgrSearch(@RequestParam(defaultValue = "1") Integer pageNum, Map m,Post post,HttpSession session){
        if(post!=(session.getAttribute("search"))){
            session.setAttribute("search",post);
        }
        PageInfo<Post> p=ps.mgrSearch(pageNum,4,post);
        m.put("p",p);
        return "/post/postMgr/list";
    }

    @RequestMapping("/lookPost")
    public String lookPost(Integer id,HttpSession session,Map m){
        //该帖子点击量加1
        ps.addClick(id);
        //用户_点击表增加一条记录
        User nowUser=(User) session.getAttribute("theUser");
        ps.addUserClick(id,nowUser.getId());
        //找到该帖子
        Post p=ps.lookPost(id);
        m.put("p",p);
        //找到点赞该帖子的人
        List<Integer> goodList=ps.queryGoodId(id);
        m.put("goodList",goodList);
        return "/post/lookPost";

    }
    //点赞
    @RequestMapping("/doSupport")
    public String doSupport(Integer id,HttpSession session,Map m){
        //该帖子点赞量加1
        ps.addSupport(id);
        //用户_点赞表增加一条记录
        User nowUser=(User) session.getAttribute("theUser");
        ps.addUserSupport(id,nowUser.getId());
        //找到该帖子
        Post p=ps.lookPost(id);
        m.put("p",p);
        //找到点赞该帖子的人
        List<Integer> goodList=ps.queryGoodId(id);
        m.put("goodList",goodList);
        return "/post/lookPost";
    }

    //取消点赞
    @RequestMapping("/cancelSupport")
    public String cancelSupport(Integer id,HttpSession session,Map m){
        //该帖子点赞量减1
        ps.cancelSupport(id);
        //用户_点赞表删除一条记录
        User nowUser=(User) session.getAttribute("theUser");
        ps.delUserSupport(id,nowUser.getId());
        //找到该帖子
        Post p=ps.lookPost(id);
        m.put("p",p);
        //找到点赞该帖子的人
        List<Integer> goodList=ps.queryGoodId(id);
        m.put("goodList",goodList);
        return "/post/lookPost";
    }
    //进个人主页
    @RequestMapping("/homePage")
    public String homePage(Integer homeId,Map m){
        //找到该人
        User homeUser=ps.queryHomeUser(homeId);
        m.put("homeUser",homeUser);
        //找到该人的帖子
        List<Post> plist=ps.homePost(homeId);
        m.put("plist",plist);
        //查出该人获得的点赞数
        int homeSupports=ps.homeSupports(homeId);
        m.put("homeSupports",homeSupports);
        return "/post/homePage";
    }


    //置顶
    @RequestMapping("/top")
    public String top(Integer id){
        ps.top(id);
        return "redirect:/post/mgrShow";
    }
    //取消置顶
    @RequestMapping("/cancelTop")
    public String cancelTop(Integer id){
        ps.cancelTop(id);
        return "redirect:/post/mgrShow";
    }

    //加精
    @RequestMapping("/good")
    public String good(Integer id){
        ps.good(id);
        return "redirect:/post/mgrShow";
    }

    //取消加精
    @RequestMapping("/cancelGood")
    public String cancelGood(Integer id){
        ps.cancelGood(id);
        return "redirect:/post/mgrShow";
    }
    //封禁
    @RequestMapping("/ban")
    public String ban(Integer id){
        ps.ban(id);
        return "redirect:/post/mgrShow";
    }
    //取消封禁
    @RequestMapping("/cancelBan")
    public String cancelBan(Integer id){
        ps.cancelBan(id);
        return "redirect:/post/mgrShow";
    }

    //页面跳转
    @RequestMapping("/toHome")
    public String toHome(HttpSession session){
        //查出有几种类型的帖子
        List<Posttype> ptlist = pts.queryAll();
        session.setAttribute("ptlist",ptlist);
        return  "/post/home";
    }
    //去后台
    @RequestMapping("/toBack")
    public String toBack(){
        return "/admin/home";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Map m){
        return "/post/addPost";
    }

    @RequestMapping("/toLookpost")
    public String toLookpost(){
        return  "/post/lookPost";
    }

    @RequestMapping("/toIndex")
    public String toIndex(){
        return  "/post/index";
    }
}
